﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace QD.Tools
{
    public static class GeneralTools
    {
        /// <summary>
        /// 26个字母数组
        /// </summary>
        public static string[] AllLetters = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

        /// <summary>
        /// 换行符
        /// </summary>
        public static string EnterStrings = "\r\n";

        [DllImport("User32.DLL")]
        public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, string lParam);
        [DllImport("User32.DLL")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        public const uint WM_SETTEXT = 0x000C;
        /// <summary>
        /// 打开事本，并添加内容
        /// </summary>
        /// <param name="context"></param>
        public static void OpenNotepad(string context)
        {
            Process vProcess = Process.Start("notepad.exe");
            while (vProcess.MainWindowHandle == IntPtr.Zero) vProcess.Refresh();
            IntPtr vHandle = FindWindowEx(vProcess.MainWindowHandle,
                IntPtr.Zero, "Edit", null);
            SendMessage(vHandle, WM_SETTEXT, 0, context);
        }

        /// <summary>
        /// 提示EF实体验证失败详细信息
        /// </summary>
        /// <param name="ex"></param>
        public static void EFValidaPrompt(DbEntityValidationException ex)
        {
            StringBuilder errStr = new StringBuilder();
            List<DbEntityValidationResult> resultList = (List<DbEntityValidationResult>)ex.EntityValidationErrors;
            foreach (DbEntityValidationResult result in resultList)
            {
                ICollection<DbValidationError> errorList = result.ValidationErrors;
                foreach (DbValidationError error in errorList)
                {
                    errStr.Append(error.ErrorMessage + "\r\n");
                }
            }

            PromptHelper.Prompt(errStr.ToString());
        }

        /// <summary>
        /// 是否为系统按键(Shift+Ctrl+Alt+F8)
        /// </summary>
        /// <returns></returns>
        public static bool IsSystemKeysF8(KeyEventArgs e)
        {
            if ((Control.ModifierKeys & Keys.Control) == Keys.Control
                 && (Control.ModifierKeys & Keys.Shift) == Keys.Shift
                 && (Control.ModifierKeys & Keys.Alt) == Keys.Alt
                 && e.KeyCode == Keys.F8)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 是否为系统按键(Shift+Ctrl+Alt+F9)
        /// </summary>
        /// <returns></returns>
        public static bool IsSystemKeysF9(KeyEventArgs e)
        {
            if ((Control.ModifierKeys & Keys.Control) == Keys.Control
                 && (Control.ModifierKeys & Keys.Shift) == Keys.Shift
                 && (Control.ModifierKeys & Keys.Alt) == Keys.Alt
                 && e.KeyCode == Keys.F9)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 取sql与c#的类型映射集合
        /// </summary>
        /// <returns></returns>
        public static Hashtable GetDataTypeMapList()
        {
            Hashtable DataTypeMapList = new Hashtable();

            #region 添加sql到c#类型映射
            DataTypeMapList.Add("varchar", "string");
            DataTypeMapList.Add("nvarchar", "string");
            DataTypeMapList.Add("char", "string");
            DataTypeMapList.Add("nchar", "string");
            DataTypeMapList.Add("text", "string");
            DataTypeMapList.Add("longtext", "string");
            DataTypeMapList.Add("ntext", "string");
            DataTypeMapList.Add("string", "string");
            DataTypeMapList.Add("date", "DateTime");
            DataTypeMapList.Add("datetime", "DateTime");
            DataTypeMapList.Add("smalldatetime", "DateTime");
            DataTypeMapList.Add("smallint", "int");
            DataTypeMapList.Add("int", "int");
            DataTypeMapList.Add("number", "int");
            DataTypeMapList.Add("bigint", "long");
            DataTypeMapList.Add("float", "decimal");
            DataTypeMapList.Add("numeric", "decimal");
            DataTypeMapList.Add("decimal", "decimal");
            DataTypeMapList.Add("money", "decimal");
            DataTypeMapList.Add("smallmoney", "decimal");
            DataTypeMapList.Add("real", "decimal");
            DataTypeMapList.Add("bit", "bool");
            DataTypeMapList.Add("binary", "byte[]");
            DataTypeMapList.Add("varbinary", "byte[]");
            DataTypeMapList.Add("image", "byte[]");
            DataTypeMapList.Add("long", "Int64");
            DataTypeMapList.Add("blob", "byte[]");
            DataTypeMapList.Add("tinyint", "Int16");
            DataTypeMapList.Add("bfile", "byte[]");
            DataTypeMapList.Add("uniqueidentifier", "Guid");
            DataTypeMapList.Add("integer", "int");
            DataTypeMapList.Add("double", "decimal");
            DataTypeMapList.Add("enum", "Enum");
            DataTypeMapList.Add("timestamp", "DateTime");
            #endregion

            return DataTypeMapList;
        }
    }
}
